// 出入栏路由处理函数
const db = require('../db/index');

// 获得出入栏列表
exports.getMovementList = (req, res) => {
    const phone = req.query.phone;
    const sql = 'select id,cattle_id,movement_type,movement_date,reason,weight,health_status from cattle_movement where phone=?';
    db.query(sql, phone,(err, results) => {
        if (err) return res.cc(err);
        res.send({
            code: 0,
            message: '获取出入栏列表成功',
            data: results
        })
    })
}

// 添加出入栏
// 添加出入栏接口
exports.addMovement = (req, res) => {
    const { cattle_id, movement_type, movement_date, reason, weight, health_status, phone } = req.body;

    const sql = 'INSERT INTO cattle_movement (cattle_id, movement_type, movement_date, reason, weight, health_status, phone) VALUES (?, ?, ?, ?, ?, ?, ?)';
    
    db.query(sql, [cattle_id, movement_type, movement_date, reason, weight, health_status, phone], (err, results) => {
      if (err) return res.cc(err);
      res.send({
        code: 0,
        message: '添加出入栏成功'
      });
    });
  };
// 删除出入栏
exports.deleteMovement = (req, res) => {
    const id = req.query.id;
    const sql = 'delete from cattle_movement where id=?';
    db.query(sql, id, (err, results) => {
        if (err) return res.cc(err);
        res.send({
            code: 0,
            message: '删除出入栏成功'
        })
    })
}